package com.huang.lesson03;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TestSocker {
    public static void main(String[] args) {
        Socket socket =null;
        InputStream is=null;
        FileOutputStream stream =null;
        OutputStream outputStream =null;

        try {
            ServerSocket serverSocket = new ServerSocket(90);
             socket = serverSocket.accept();//阻塞监听   会等待客户端一直链接
             is = socket.getInputStream();

            stream = new FileOutputStream("C:/Users/HJW/Pictures/h1.jpg");
            byte[] bytes = new byte[1024];
            int len;
            while ((len=is.read(bytes))!=-1){
                    stream.write(bytes,0,len);
            }
            //接受完消息给客户端发送消息 说接受完了  饭后在断开
            outputStream = socket.getOutputStream();
            outputStream.write("我接受玩了   可以断开了 ".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
